package com.kcs.fxmargin.service;

import java.sql.Connection;

import org.apache.log4j.Logger;

import com.authen.ldap.KCS_LDAPEntry;
import com.authen.ldap.LDAPUtil;
import com.kcs.common.utils.Based64Utils;
import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.bo.LoginBO;
import com.kcs.fxmargin.dao.LoginDAO;

public class LoginService {
	
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
    public LoginBO authenticateUserLDAP(LoginBO bo) throws Exception {
        LDAPUtil test = null;
        KCS_LDAPEntry ent = null;
        
        log.info("Begin : LoginService : authenticateUserLDAP");
        
        try
        {
            if(validateModel(bo))
            {
            	test = new LDAPUtil();
              	ent = test.authenticateUser(bo.getUserId(), bo.getPassword(), bo.getUserId());
                
                if(ent != null){
                	bo.setPassword(Based64Utils.base64encode(bo.getPassword()));
                	bo.setResultLdap(true);
                }else{
                	bo.setResultLdap(false);
                }
                
            }else{
            	bo.setResultLdap(false);
            }
        }
        catch(Exception e)
        {
        	bo.setResultLdap(false);
            throw e;
        }
        
        log.info("End : LoginService : authenticateUserLDAP");
        return bo;
    }

    public boolean validateModel(LoginBO bo)throws Exception {
    	log.info("Begin : LoginService : validateModel");
        if(bo.getUserId().equals("")){
            return false;
        }

        log.info("End : LoginService : validateModel");
        return !bo.getPassword().equals("");
    }
    
    public LoginBO getUserData(LoginBO bo)throws Exception{
        Connection conn = null;
        LoginDAO dao = null;
        
        log.info("Begin : LoginService : getUserData");
        
        try{
            if(validateGetUserData(bo)){
                conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO);
                dao = new LoginDAO();
                bo = dao.getUserData(bo, conn);
            }
        }
        catch(Exception e)
        {
            throw e;
        }finally{
        	if(conn != null){
	            try{
	                conn.close();
	            }catch(Exception ex){
	                throw ex;
	            }
        	}
        }
        
        log.info("End : LoginService : getUserData");
        return bo;
	}
	
	public boolean validateGetUserData(LoginBO bo)throws Exception {
		log.info("Begin : LoginService : validateGetUserData");
		log.info("End : LoginService : validateGetUserData");
		return !bo.getUserId().equals("");
	}
	
	public LoginBO getMenuList(LoginBO bo) throws Exception {
        Connection conn = null;
        LoginDAO dao = null;
        
        log.info("Begin : LoginService : getMenuList");
        
        try{
            if(validateGetUserListModel(bo)){
                conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO);
                dao = new LoginDAO();
                bo = dao.getMenuList(bo, conn);
            }else{
            	throw new Exception(Constant.ERR_ROLE_ID_EMPTY);
            }
        }
        catch(Exception e)
        {
            throw e;
        }finally{
        	if(conn != null){
	            try{
	                conn.close();
	            }catch(Exception ex){
	                throw ex;
	            }
        	}
        }
        
        log.info("End : LoginService : getMenuList");
        return bo;
    }
	
	public boolean validateGetUserListModel(LoginBO bo) throws Exception {
		log.info("Begin : LoginService : validateGetUserListModel");
		log.info("End : LoginService : validateGetUserListModel");
		return !bo.getRoleId().equals("");
	}
	
	 public static void main(String args[]) {
	    	for (int i = 0; i < 1; i++) {
	    		Connection  conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO);
	            if (conn != null){
	            	System.out.println(conn);
	            }
			}
	        
	    }   
}